package com.yzxb.messystem.controller;

import com.yzxb.messystem.entity.ProductionRecord;
import com.yzxb.messystem.exception.ResourceNotFoundException;
import com.yzxb.messystem.exception.BusinessException;
import com.yzxb.messystem.service.ProductionRecordService;
import com.yzxb.messystem.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/production-records")
public class ProductionRecordController {
    
    @Autowired
    private ProductionRecordService productionRecordService;
    
    // 获取所有生产记录
    @GetMapping
    public ResponseResult<List<ProductionRecord>> getAllProductionRecords() {
        List<ProductionRecord> productionRecords = productionRecordService.getAllProductionRecords();
        return ResponseResult.success("获取生产记录列表成功", productionRecords);
    }
    
    // 根据ID获取生产记录
    @GetMapping("/{id}")
    public ResponseResult<ProductionRecord> getProductionRecordById(@PathVariable Long id) {
        try {
            ProductionRecord productionRecord = productionRecordService.getProductionRecordById(id)
                    .orElseThrow(() -> new ResourceNotFoundException("生产记录不存在: " + id));
            return ResponseResult.success("获取生产记录成功", productionRecord);
        } catch (ResourceNotFoundException e) {
            return ResponseResult.error(404, e.getMessage());
        }
    }
    
    // 添加生产记录
    @PostMapping
    public ResponseResult<ProductionRecord> addProductionRecord(@RequestBody ProductionRecord record) {
        try {
            ProductionRecord newRecord = productionRecordService.addProductionRecord(record);
            return ResponseResult.success("添加生产记录成功", newRecord);
        } catch (BusinessException e) {
            return ResponseResult.error(400, e.getMessage());
        }
    }
    
    // 更新生产记录
    @PutMapping("/{id}")
    public ResponseResult<ProductionRecord> updateProductionRecord(@PathVariable Long id, @RequestBody ProductionRecord record) {
        try {
            ProductionRecord updatedRecord = productionRecordService.updateProductionRecord(id, record);
            return ResponseResult.success("更新生产记录成功", updatedRecord);
        } catch (RuntimeException e) {
            return ResponseResult.error(404, "生产记录不存在");
        }
    }
    
    // 删除生产记录
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteProductionRecord(@PathVariable Long id) {
        try {
            productionRecordService.deleteProductionRecord(id);
            return ResponseResult.success("删除生产记录成功", null);
        } catch (RuntimeException e) {
            return ResponseResult.error(404, "生产记录不存在");
        }
    }
    
    // 根据工单ID查询生产记录
    @GetMapping("/work-order/{workOrderId}")
    public ResponseResult<List<ProductionRecord>> getProductionRecordsByWorkOrderId(@PathVariable Long workOrderId) {
        List<ProductionRecord> productionRecords = productionRecordService.getProductionRecordsByWorkOrderId(workOrderId);
        return ResponseResult.success("按工单ID查询生产记录成功", productionRecords);
    }
    
    // 根据记录类型查询生产记录
    @GetMapping("/type/{recordType}")
    public ResponseResult<List<ProductionRecord>> getProductionRecordsByRecordType(@PathVariable ProductionRecord.RecordType recordType) {
        List<ProductionRecord> productionRecords = productionRecordService.getProductionRecordsByRecordType(recordType);
        return ResponseResult.success("按记录类型查询生产记录成功", productionRecords);
    }
    
    // 根据工作站查询生产记录
    @GetMapping("/workstation/{workstation}")
    public ResponseResult<List<ProductionRecord>> getProductionRecordsByWorkstation(@PathVariable String workstation) {
        List<ProductionRecord> productionRecords = productionRecordService.getProductionRecordsByWorkstation(workstation);
        return ResponseResult.success("按工作站查询生产记录成功", productionRecords);
    }
    
    // 根据操作员查询生产记录
    @GetMapping("/operator/{operator}")
    public ResponseResult<List<ProductionRecord>> getProductionRecordsByOperator(@PathVariable String operator) {
        List<ProductionRecord> productionRecords = productionRecordService.getProductionRecordsByOperator(operator);
        return ResponseResult.success("按操作员查询生产记录成功", productionRecords);
    }
    
    // 根据班次查询生产记录
    @GetMapping("/shift/{shift}")
    public ResponseResult<List<ProductionRecord>> getProductionRecordsByShift(@PathVariable String shift) {
        List<ProductionRecord> productionRecords = productionRecordService.getProductionRecordsByShift(shift);
        return ResponseResult.success("按班次查询生产记录成功", productionRecords);
    }
    
    // 根据时间范围查询生产记录
    @GetMapping("/time-range")
    public ResponseResult<List<ProductionRecord>> getProductionRecordsByTimeRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        List<ProductionRecord> productionRecords = productionRecordService.getProductionRecordsByTimeRange(startTime, endTime);
        return ResponseResult.success("按时间范围查询生产记录成功", productionRecords);
    }
    
    // 添加生产数量记录
    @PostMapping("/production")
    public ResponseResult<ProductionRecord> addProductionQuantity(
            @RequestParam Long workOrderId,
            @RequestParam Integer quantity,
            @RequestParam String workstation,
            @RequestParam String operator,
            @RequestParam String shift) {
        try {
            ProductionRecord record = productionRecordService.addProductionQuantity(workOrderId, quantity, workstation, operator, shift);
            return ResponseResult.success("添加生产数量记录成功", record);
        } catch (RuntimeException e) {
            return ResponseResult.error(400, e.getMessage());
        }
    }
    
    // 添加不合格数量记录
    @PostMapping("/rejection")
    public ResponseResult<ProductionRecord> addRejectionQuantity(
            @RequestParam Long workOrderId,
            @RequestParam Integer quantity,
            @RequestParam String workstation,
            @RequestParam String operator,
            @RequestParam String shift,
            @RequestParam(required = false) String qualityStatus,
            @RequestParam(required = false) String remarks) {
        try {
            ProductionRecord record = productionRecordService.addRejectionQuantity(workOrderId, quantity, workstation, operator, shift, qualityStatus, remarks);
            return ResponseResult.success("添加不合格数量记录成功", record);
        } catch (RuntimeException e) {
            return ResponseResult.error(400, e.getMessage());
        }
    }
}